package calendar;

import calendar.messages.*;
import edu.gmu.messages.EndPoint;
import edu.gmu.middleware.Message;
import edu.gmu.middleware.RemoteStub;
import java.io.IOException;
import java.util.ArrayList;

/**
 * NOTE: STATELESS
 * @author james
 */
public class CalendarMgrStub implements CalendarMgrInt
{
    private EndPoint endpoint = null;
    
    /**
     * Creates a new CalendarStub with specified params.
     */
    public CalendarMgrStub( EndPoint endpoint )
    {
        this.endpoint    = endpoint;

        
    }
    
    private RemoteStub makeStub() throws IOException
    {
        RemoteStub stub = new RemoteStub(   endpoint.address,
                                            endpoint.port,
                                            endpoint.port,
                                            endpoint.address );
        stub.register(Topic.CREATE_CALENDAR_RESPONSE, Result.class);
        stub.register(Topic.LIST_RESPONSE,       ListResponse.class);
        stub.register(Topic.CONNECT_RESPONSE,    ConnectResponse.class);
        stub.register(Topic.DISCONNECT_RESPONSE, DisconnectResponse.class);
        stub.register(Topic.ALIVE_RESPONSE,      AliveResponse.class);
        
        return stub;
    }
    

    public boolean createCalendar(User user) throws IOException
    {
        // Connect
        RemoteStub stub = this.makeStub();
        
        Message message = stub.execute(Topic.CREATE_CALENDAR_REQUEST, user);
        Result result = (Result)message;
        
        // Disconnect
        stub.stop();
        
        return result.result;
    }

    public boolean connectCalendar(User user) throws IOException
    {
        // Connect
        RemoteStub stub = this.makeStub();
        
        ConnectRequest request = new ConnectRequest();
        request.user = user;
        Message message = stub.execute(Topic.CONNECT_REQUEST, request);
        ConnectResponse response = (ConnectResponse)message;
        
        // Disconnect
        stub.stop();
        
        return response.result;
    }

    public boolean disconnectCalendar(User user) throws IOException
    {
        // Connect
        RemoteStub stub = this.makeStub();
        
        DisconnectRequest request = new DisconnectRequest();
        request.user = user;
        Message message = stub.execute(Topic.DISCONNECT_REQUEST, request);
        DisconnectResponse response = (DisconnectResponse)message;
        
        // Disconnect
        stub.stop();
        
        return response.result;
    }

    public ArrayList list() throws IOException
    {
        // Connect
        RemoteStub stub = this.makeStub();
        
        ListRequest request = new ListRequest();
        Message message = stub.execute(Topic.LIST_REQUEST, request);
        ListResponse response = (ListResponse)message;
        
        // Disconnect
        stub.stop();
        
        return response.getUsers();
    }

    public void isAlive() throws IOException
    {
        // Connect
        RemoteStub stub = this.makeStub();
        
        AliveRequest request = new AliveRequest();
        Message message = stub.execute(Topic.ALIVE_REQUEST, request);
        AliveResponse response = (AliveResponse)message;
        
        // Disconnect
        stub.stop();
        
        return;
    }
}
